package util

import (
	"github.com/dlclark/regexp2"
	"github.com/pkg/errors"
	"strings"
	"time"
)

const (
	stringConnect = "#"
	InvalidString = ""
	EmptyString   = ""
	InvalidNumber = -1
)

func MatchStr(pattern, str string) (bool, error) {
	reg, err := regexp2.Compile(pattern, 0)
	if err != nil {
		return false, errors.Errorf("MatchStr error: %v", err)
	}
	m, err := reg.FindStringMatch(str)
	if err != nil {
		return false, errors.Errorf("MatchStr error: %v", err)
	}
	if m != nil {
		return true, nil
	}
	return false, nil
}

func CombineTwoString(firstString, secondString string) string {
	//Concat
	var builder strings.Builder
	builder.WriteString(firstString)
	builder.WriteString(secondString)
	return builder.String()
}

func CombineStringWithConnect(partString []string) string {
	//Concat
	var builder strings.Builder
	for i := 0; i < len(partString); i++ {
		builder.WriteString(partString[i])
		if i != len(partString)-1 {
			builder.WriteString(stringConnect)
		}
	}
	return builder.String()
}
func GetStringFromConnect(oldString string) []string {
	ss := strings.Split(oldString, stringConnect)
	return ss
}
func InArray(target int, source []int) bool {
	for _, each := range source {
		if target == each {
			return true
		}
	}
	return false
}
func InStringArray(target string, source []string) bool {
	for _, each := range source {
		if target == each {
			return true
		}
	}
	return false
}

func GetDefaultTime() time.Time {
	return GetZeroTime(1970, 1, 1)
}

func GetZeroTime(year int, month time.Month, day int) time.Time {
	// 获取指定日期的0点时间
	return time.Date(year, month, day, 0, 0, 0, 0, time.Local)
}

func GetMilliTime(time time.Time) int64 {
	return time.UnixMilli()
}
